MySql数据库的语句

您所在的位置:网站首页 sql 语句 去掉重复项 MySql数据库的语句

MySql数据库的语句

2023-06-11 12:10| 来源: 网络整理| 查看: 265

第一范式(1NF):

第一范式是指数据库的每一列都是不可分割的基本数据项,而下面这样的就存在可分割的情况:

学生(姓名,电话号码)

学生(姓名,座机号码,手机号码)

第二范式(2NF):

第二范式要求表中必须存在主键,且其他的属性必须完全依赖于主键

学生(学号,姓名,性别)

第三范式(3NF):

在满足第二范式的情况下,所有的属性都不传递依赖于主键,满足第三范式。

学生借书情况(借阅编号,学生学号,书籍编号,书籍名称,书籍作者)

实际上书籍编号依赖于借阅编号,而书籍名称和书籍作者依赖于书籍编号,因此存在传递依赖的情况,我们可以将书籍信息进行单独拆分为另一张表:

学生借书情况(借阅编号,学生学号,书籍编号)

书籍(书籍编号,书籍名称,书籍作者)

SQL:

如果出现错误的话Communications link failure

services.msc在文件中输入可用来启动

直接重新启动一下MySQL 可以是5也可以是8

sql语句: 建立外键:

constraint f_tid foreign key (tid) references teacher(tid),建立一个外键constraint(约束)foreign key(外键)references(参考)

图像化实例:

create database mysql;//创建一个数据库mysql drop database mysql1;//删除数据库mysql //新建查询控制台 可以用来查询 use mysql1; //使用数据库 create table table1(id int primary key ,name varchar(10) not null ,six enum('男','女') not null default '男');//创建表 //not null默认不为空 enum为枚举型数据 default默认的数据 drop table table2;//删除table的语句 create table untitled(tid int not null ,sid int not null ,constraint 'f_tid' foreign key ('tid') references 'teacher' ('tid'),constraint 'f_sid' foreign key ('sid') references 'student'('id'));// create table teach( tid int not null, sid int not null, constraint f_tid foreign key (tid) references teacher(tid), constraint f_sid foreign key (sid) references student(id) ); //建立一个外键 alter table teacher add sex enum('男','女') not null default '男';//新建一个列 改表default(默认) 修改表 插入,删除,修改: insert into student values (1,'小李','男');//插入数 insert into student(id, sex,name) values (2,'男','小李'),(3,'女','小王');//插入多组数据 update student set name='小罗' where id=1;//修改数据 where是条件 delete from student where id=3;//删除数据 alter table teacher drop column sex;//删除一列

查询:

一般的比较运算符,包括=、>、=、是否在集合中:in、not in字符模糊匹配:like,not like多重条件连接查询:and、or、notcount([distinct]*)统计所有的行数(distinct表示去重再统计,下同) count([distinct]列名)统计某列的值总和sum([distinct]列名)求一列的和(注意必须是数字类型的)avg([distinct]列名)求一列的平均值(注意必须是数字类型)max([distinct]列名)求一列的最大值min([distinct]列名)求一列的最小值 select *from student where id=1;//查询数据 from来自表 select *from studentwhere id not in(1);//不在()中的数据都被打印出来 select *from student where id like '%1';//抽象like'%1'有包含的 select * from student where sex like '%男' and id=1; select *from student order by id desc ;//倒序排序desc select count(distinct sex) from student;//sex的种类 select *from student;//总共有多少 select count(*),sex from student group by sex; select sum(id)from student ; select avg(id)from student; select *from student limit 3; select teacher.name from teacher,student where id=1; select *from teacher inner join teach on teacher.tid = teach.tid; select *from student left join teach t on student.id = t.sid;//left以左边为中心 select *from teach right join student on student.id = teach.sid;//right 以右边为中心 select * from student left join teach t on student.id = t.sid left join teacher t2 on t.tid = t2.tid; select * from student left join teach t on student.id = t.sid left join teacher t2 on t.tid = t2.tid where t.tid=1;//以student为主 select *from student where id in(select sid from teach where tid in(select tid from teacher where name='小刚'));//以查询结果进行查询

 

 

视图,索引,触发器:

是表的抽象 表的虚拟

视图中的元素允许查看与修改

create view text as select * from teacher; update text set name='小黄'where tid=1; select *from text; create index si1 on student(id); show index from student;//创建索引与删除索引 create trigger i after delete on student for each row delete from student where id=6; show triggers;//创建触发器与删除触发器 begin; #开始事务 ... rollback; #回滚事务 savepoint 回滚点; #添加回滚点 rollback to 回滚点; #回滚到指定回滚点 ... commit; #提交事务 -- 一旦提交,就无法再进行回滚了!

 



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3